package com.samphanie.auiu.admin.config;

import com.samphanie.auiu.common.config.PassayPropertiesMessageResolver;
import lombok.RequiredArgsConstructor;
import org.passay.MessageResolver;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author ZSY
 * @email 1451691457@qq.com
 */
@Configuration
@RequiredArgsConstructor
public class MyWebMvcConfig implements WebMvcConfigurer {

    private final MessageSource messageSource;
    private final Environment environment;

    /**
     * 配置 Passay 使用 Spring 的 MessageSource
     * 配置自定义的 Passay 消息解析器
     *
     * @return MessageResolver
     */
    @Bean
    public MessageResolver messageResolver() {
        // return new SpringMessageResolver(messageSource);
        return new PassayPropertiesMessageResolver(messageSource);
    }

    /**
     * 配置 Java Validation 使用国际化的消息资源
     * @return LocalValidatorFactoryBean
     */
    @Bean
    public LocalValidatorFactoryBean localValidatorFactoryBean() {
        LocalValidatorFactoryBean localValidatorFactoryBean = new LocalValidatorFactoryBean();
        localValidatorFactoryBean.setValidationMessageSource(messageSource);
        return localValidatorFactoryBean;
    }


}
